package cc.ufinity.networkdiagnose.widget;

import cc.ufinity.networkdiagnose.manager.DBManager;
import cc.ufinity.networkdiagnose.manager.FTPConfig;
import cc.ufinity.networkdiagnose.util.StringUtils;
import cc.ufinity.networkdiagnose.view.R;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddressConfigWidget extends GeneralTestWidget {
	private Button backBtn;
	private Button okBtn;
	private Activity ctx;
	private EditText userText;
	private EditText pwdText;
	private EditText addressText;

	public AddressConfigWidget(Activity context) {
		super(context);
		inflate(context, R.layout.address_config_widget, this);
		this.ctx = context;
		backBtn = (Button) this.findViewById(R.id.ftp_config_btnback);
		userText = (EditText) this.findViewById(R.id.ftpuser_text);
		pwdText = (EditText) this.findViewById(R.id.ftppwd_text);
		addressText = (EditText) this.findViewById(R.id.ftpaddress_text);
		okBtn = (Button)this.findViewById(R.id.ftpBtnOk);
		backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				backToConfig(ctx);
			}
		});
		okBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				saveConfig();
			}
		});
		init();
	}
	
	private void init(){
		DBManager db = new DBManager(ctx);
		db.release();
		FTPConfig config = db.getFtp2();
//		db.saveFtp(config);
		if(null != config){
			userText.setText(config.userName);
			pwdText.setText(config.password);
			addressText.setText(config.address);
		}
	}

	private void saveConfig(){
		String user = userText.getText().toString();
		String pwd = pwdText.getText().toString();
		String address = addressText.getText().toString();
		if(StringUtils.isEmpty(user) || StringUtils.isEmpty(pwd) || StringUtils.isEmpty(address)){
			this.showMessage(ctx, "请输入完整信息.");
			return;
		}
		DBManager db = new DBManager(ctx);
		FTPConfig config = new FTPConfig();
		config.port = 21;
		config.userName = user;
		config.password = pwd;
		config.address = address;
		if(db.saveFtp(config)){
			this.showMessage(ctx, "FTP配置已保存.");
		}else{
			this.showMessage(ctx, "FTP配置保存出错.");
		}
		db.release();
		
	}
}
